Add fallback glyphs for GtkSwitch states
authorEmmanuele Bassi <ebassi@gnome.org>
Tue, 15 Jan 2019 22:16:05 +0000 (22:16 +0000)
committerEmmanuele Bassi <ebassi@gnome.org>
Tue, 15 Jan 2019 22:16:05 +0000 (22:16 +0000)
Even though the IEC power glyphs are part of Unicode 9.0 (released in
2016) not all fonts have them.

To avoid showing the hexbox of doom when the system font does not have
the glyphs we'd like to use, add a fallback pair, using the old glyphs
we suggested when the labels were translatable.

gtk/gtkswitch.c

index abc37ab8637a56aceb7954ccf06b1b763f34ae9b..cb361c129228786ec8e8e6875ee7efbcbdb30362 100644 (file)
@@ -280,6 +280,54 @@ gtk_switch_activate (GtkSwitch *sw)
   gtk_switch_begin_toggle_animation (sw);
 }
 
+static void
+gtk_switch_update_state_labels (GtkSwitch *sw)
+{
+  /* Glyphs for the ON state, in descending order of preference */
+  const char *on_glyphs[] = {
+    "⏽", /* U+23FD POWER ON SYMBOL */
+    "❙", /* U+2759 MEDIUM VERTICAL BAR */
+  };
+
+  /* Glyphs for the OFF state, in descending order of preference */
+  const char *off_glyphs[] = {
+    "⭘", /* U+2B58 HEAVY CIRCLE */
+    "○", /* U+25CB WHITE CIRCLE */
+  };
+
+  GtkSwitchPrivate *priv = gtk_switch_get_instance_private (sw);
+  GtkWidget *widget = GTK_WIDGET (sw);
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (on_glyphs); i++)
+    {
+      PangoLayout *layout = gtk_widget_create_pango_layout (widget, on_glyphs[i]);
+
+      if (pango_layout_get_unknown_glyphs_count (layout) == 0)
+        {
+          gtk_label_set_text (GTK_LABEL (priv->on_label), on_glyphs[i]);
+          g_object_unref (layout);
+          break;
+        }
+
+      g_object_unref (layout);
+    }
+
+  for (i = 0; i < G_N_ELEMENTS (off_glyphs); i++)
+    {
+      PangoLayout *layout = gtk_widget_create_pango_layout (widget, off_glyphs[i]);
+
+      if (pango_layout_get_unknown_glyphs_count (layout) == 0)
+        {
+          gtk_label_set_text (GTK_LABEL (priv->off_label), off_glyphs[i]);
+          g_object_unref (layout);
+          break;
+        }
+
+      g_object_unref (layout);
+    }
+}
+
 static void
 gtk_switch_measure (GtkWidget      *widget,
                     GtkOrientation  orientation,
@@ -639,14 +687,17 @@ gtk_switch_init (GtkSwitch *self)
   gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (gesture));
   priv->pan_gesture = gesture;
 
-  priv->on_label = gtk_label_new ("⏽");
+
+  priv->on_label = gtk_label_new ("");
   gtk_widget_set_parent (priv->on_label, GTK_WIDGET (self));
 
-  priv->off_label = gtk_label_new ("");
+  priv->off_label = gtk_label_new ("");
   gtk_widget_set_parent (priv->off_label, GTK_WIDGET (self));
 
   priv->slider = gtk_gizmo_new ("slider", NULL, NULL, NULL);
   gtk_widget_set_parent (priv->slider, GTK_WIDGET (self));
+
+  gtk_switch_update_state_labels (self);
 }
 
 /**